Allows you to create documents such as Word, html, pdf and PowerPoint directly from R incorporating outputs and code directly into the document.
Creating an R Markdown document
File -> New File… -> R Markdown..
Note: You can change between file types at a later time.
RStudio will have created a template. Consisting of a YAML and examples.
-–
title: “RMarkdown Tutorial”
author: “Tom Dougall”
date: “08/11/2021”
output: html_document
-–
To create your output file, click ‘Knit’ at the top of the script.
#’s before text will indicate it is a header, the more #s, the smaller the header
# Header 1## Header 2### Header 3*italtics* for italics OR _italics_**bold** for bold OR __bold__~~strikethrough~~ for * or - or + for a bullet point
code formatted text by using two back ticks `code`Chunks of code and their output can be included in an RMarkdown file. They are written with the following syntax;
```{r }
R code here.
```
Within the chunk header, we can set several options such as;
Creating an RMarkdown in RStudio will by default set a global options, these can be overwritten in any chunk. The default is to show all code in output
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
RStudio have a handy cheat sheet for text formatting and code chunks options. https://www.rstudio.com/wp-content/uploads/2015/02/rmarkdown-cheatsheet.pdf
Best practice is to name all your chunks, it will help you troubleshoot by tracking which chunks has broken. When naming your chunk, it must be given a unique name, if you don’t assign a name, R will assign a default name with an incremental number. When writing your chunk header, it must be on a single line.
```{r cars-plot, include=TRUE}
data(mtcars)
plot(cars$speed, cars$dist)
```
Beyond the options we’ve seen so far, there are a lot more we can use. We can improve out plot by adding a label cars-plot and a title in the header {r label=cars-plot, fig.cap = "A plot showing the stopping disance of cars by the speed they were travelling"}. The title will append a figure caption to the plot.
A plot showing the stopping disance of cars by the speed they were travelling
We can also set other plot options such as
fig.widthfig.heightfig.alignBeyond plots we can also include options that impact areas such as;
Possible options are linked here: https://yihui.org/knitr/options/
You can also include outputs from code within your text. r max(cars$speed) which will output within the text (requires single closing quotation marks).
The max car speed tested is 25 from the cars dataset.
RStudio will give you a default YAML header but there are a lot of options not given by default.
title: "Exercise 1 Answers"
author: "Tom Dougall"
date: "09/11/2021"
output: html_document
The output determines what type of document you want to create, this was auto-generated when creating the R script in RStudio. Within RStudio we can change the document we wish to write by clicking the arrow next to knit and choosing an option such as ‘Knit to word’. In doing so, the YAML will automatically update.
output: html_document
to
output:
word_document: default
html_document: default
In the future when we knit - two documents will be created, a word and html version.
Some other option examples include;
Some options affect the styling of the document so need to be contained within the output setting.
output:
html_document:
* `toc: true` - will add a contents page
* `number_sections: true` - will number your contents page
Themes: You can apply themes to your document, some are already included such as ‘cerulean’ and others have been developed within packages such as the ones found here: https://www.datadreaming.org/post/r-markdown-theme-gallery/. Alternatively you can use previously made documents to apply themes to your outputs.
output:
html_document:
theme: "default", "bootstrap", "cerulean", "cosmo", "darkly", "flatly", "journal", "lumen", "paper", "readable", "sandstone", "simplex", "spacelab", "united", "yeti"
output: rmdformats::readthedown
output:
word_document:
reference_docx: word_template.docx
By default, RMarkdown will display output from your chunks as they’d appear in your console. Tables can rendered using your template’s style or with additional formatting by using the knitr::kable function.
knitr::kable(head(cars))
| speed | dist |
|---|---|
| 4 | 2 |
| 4 | 10 |
| 7 | 4 |
| 7 | 22 |
| 8 | 16 |
| 9 | 10 |
html documents allow you to insert interactive elements, such as tables and plots. If you are familiar with RShiny then you may have come across them before.
The ‘DT’ package will give you filterable and sortable tables.
library(DT)
datatable(cars)
The ‘plotly’ package will give you plots with features such as zooming in, or hovering over points.
library(plotly)
## Loading required package: ggplot2
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library(ggplot2)
plotExample <- ggplot( cars, aes(x=speed, y= dist)) +
geom_point()
ggplotly(plotExample)